#ifndef BASERTSPSERVER_INCLUDED
#define BASERTSPSERVER_INCLUDED

#include "base.h"
#include "BaseTcp.h"
#include "RTSPServerSession.h"

const int  RTSP_REQUEST_SIZE = 4096;

template<typename CLIENT_CONNECTION>
class BaseRtspServer: public BaseAcceptor
{
public:
	BaseRtspServer(Proactor* proactor,unsigned short listenport=RTSPSession::RTSP_PORT):BaseAcceptor(proactor,listenport){};
	~BaseRtspServer(){};
	pj_bool_t OnAccept(Proactor* proactor,pj_sock_t newsock,const pj_sockaddr_t *src_addr,int src_addr_len)
	{
		char buf[100];
		pj_sockaddr_print(src_addr,buf,sizeof(buf)-1,1);
		printf("BaseRtspServer::OnAccept client sock=%d addr=%s\n",newsock,buf);

		CLIENT_CONNECTION *pRtspClientConnection = new CLIENT_CONNECTION(proactor,newsock);
		p_proactor->RegisterHandle(pRtspClientConnection);
		pRtspClientConnection->Recv(RTSP_REQUEST_SIZE);

		return PJ_TRUE;
	};
};

class BASE_API RtspClientConnection:public BaseStream,public RTSPServerSession
{
public:
	RtspClientConnection(Proactor* proactor,pj_sock_t newsock);
	~RtspClientConnection();
protected:
	virtual pj_bool_t OnDataRead(void *data,pj_size_t size,int status,pj_size_t* remainder);
	virtual pj_bool_t OnDataSend(pj_ioqueue_op_key_t *send_key,int sendsize);
protected:
	////subclass implement below virtual method
	//virtual void OnDescribeRequest(RTSPRequest& request);
	//virtual void OnSetupRequest(RTSPRequest& request);
	//virtual void OnPlayRequest(RTSPRequest& request);
	//virtual void OnPauseRequest(RTSPRequest& request);
	//virtual void OnRecordRequest(RTSPRequest& request);
	//virtual void OnTeardownRequest(RTSPRequest& request);
	//virtual void OnClose(int status){//delete child object by code<delete this>};

};

#endif
